import bpy


def world_to_camera_view(obj):
    coord= obj.location
    scene = bpy.context.scene
    cam = bpy.data.objects["Camera"]
    co_local = cam.matrix_world.normalized().inverted() @ coord
    z = -co_local.z
    camera = cam.data
    frame = [v for v in camera.view_frame(scene=scene)[:3]]
    if camera.type != 'ORTHO':
        if z == 0.0:
            return (0.5, 0.5, 0.0)
        else:
            frame = [-(v / (v.z / z)) for v in frame]

    min_x, max_x = frame[2].x, frame[1].x
    min_y, max_y = frame[1].y, frame[0].y


    x = (co_local.x - min_x) / (max_x - min_x)
    y = (co_local.y - min_y) / (max_y - min_y)

    return (x, y, z)

def camview2uv(loc):
    scene = bpy.context.scene
    render_scale = scene.render.resolution_percentage / 100
    render_size = (
        int(scene.render.resolution_x * render_scale),
        int(scene.render.resolution_y * render_scale),
    )
    print("Pixel Coords:", (
        round(loc[0] * render_size[0]),
        round((1-loc[1]) * render_size[1]),
    ))
    # print ("render size",render_size)



def from_object_to_uv(obj):
    loc = world_to_camera_view(obj)
    camview2uv(loc)


# If you want pixel coords

